import axios from "axios"

import layoutHeaderAside from "@/layout/header-aside";
const _import = require("@/libs/util.import." + process.env.NODE_ENV);

function dealJson(jsons) {
  // { path: '/', redirect: { name: 'index' }, component: layoutHeaderAside, 
  //    children: [{ path: 'index', name: 'index', meta: { auth: true }, component: _import('system/index') }]
  //  }
  const route = [];
  //{title: '测试页面', icon: 'folder-o', children: [{ path: '/page1', title: '页面 1' }]}
  const menu = []
  // [{path: "/Auth", title: "权限管理", icon: "home", redirect: "Depart",
  //   children: [{
  //     name: "Depart", path: "Depart", title: "页面1", icon: "clone", compont: "Auth/Depart", meta: {
  //       auth: true, cache: true
  //     }
  //   }]
  // }]
  if (jsons != undefined) {
    jsons.forEach(p => {
      if (p.meta == undefined) {
        p.meta = { title: p.title }
      }
      p.meta.title = p.title;

      var component = null;
      try {
        component = p.component ? _import(p.component) : layoutHeaderAside;
      } catch (error) {
        console.error('import error', error)
      }
      const r = { path: p.path, name: p.name, meta: p.meta, } //路由
      if (component != null)
        r.component = component;
      if (p.redirect) {
        r.redirect = { name: p.redirect }
      }
      const m = { title: p.title, icon: p.icon, path: p.path }  //菜单
      if (p.children) {
        const ret = dealJson(p.children)
        r.children = ret.route
        m.children = ret.menu
      }
      if (r.component != undefined)
        route.push(r);
      menu.push(m);
    });
  }
  return { route, menu }
}
export default async () => {
  var frameInRoutes = []
  //下面两个是这种样式
  var menuHeader = []
  var menuAside = []
  const res = await axios.get('/menu.json')
  const headerJson = res.data.header;
  var ret = dealJson(headerJson);
  frameInRoutes = ret.route;
  menuHeader = ret.menu;

  const asideJson = res.data.aside;
  ret = dealJson(asideJson);
  frameInRoutes = frameInRoutes.concat(ret.route);
  menuAside = menuAside.concat(ret.menu);

  return { frameInRoutes, menuHeader, menuAside }
}